package com.qujia.customer.controller;
import java.util.List;
import com.qujia.common.core.domain.entity.SysDept;
import com.qujia.common.core.domain.model.LoginUser;
import com.qujia.common.utils.DateUtils;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.system.service.ISysDeptService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qujia.common.annotation.Log;
import com.qujia.common.core.controller.BaseController;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.enums.BusinessType;
import com.qujia.customer.domain.CustomerGradeSetting;
import com.qujia.customer.service.ICustomerGradeSettingService;
import com.qujia.common.utils.poi.ExcelUtil;
import com.qujia.common.core.page.TableDataInfo;

/**
 * 客户优惠等级设置Controller
 *
 * @author qujia
 * @date 2021-01-08
 */
@RestController
@RequestMapping("/customer/setting")
public class CustomerGradeSettingController extends BaseController
{
    @Autowired
    private ICustomerGradeSettingService customerGradeSettingService;

    @Autowired
    private ISysDeptService deptService;
    /**
     * 查询客户优惠等级设置列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CustomerGradeSetting customerGradeSetting)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = deptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            customerGradeSetting.setStationIdList(list);
            customerGradeSetting.setStationId(null);
        }
        customerGradeSetting.setDelFlag("0");
        startPage();
        List<CustomerGradeSetting> settingList = customerGradeSettingService.selectCustomerGradeSettingList(customerGradeSetting);
        return getDataTable(settingList);
    }



    /**
     * 查询当前登录人员的等级
     */
    @GetMapping("/listByLogin")
    public TableDataInfo listByLogin()
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        CustomerGradeSetting customerGradeSetting=new CustomerGradeSetting();
        customerGradeSetting.setStationId(currentUser.getUser().getDeptId());
        customerGradeSetting.setDelFlag("0");
        List<CustomerGradeSetting> settingList = customerGradeSettingService.selectCustomerGradeSettingList(customerGradeSetting);
        return getDataTable(settingList);
    }

    /**
     * 导出客户优惠等级设置列表
     */
    @Log(title = "客户优惠等级设置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CustomerGradeSetting customerGradeSetting)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = deptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            customerGradeSetting.setStationIdList(list);
            customerGradeSetting.setStationId(null);
        }
        customerGradeSetting.setDelFlag("0");
        List<CustomerGradeSetting> settingList = customerGradeSettingService.selectCustomerGradeSettingList(customerGradeSetting);
        ExcelUtil<CustomerGradeSetting> util = new ExcelUtil<CustomerGradeSetting>(CustomerGradeSetting.class);
        return util.exportExcel(settingList, "setting");
    }

    /**
     * 获取客户优惠等级设置详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(customerGradeSettingService.selectCustomerGradeSettingById(id));
    }

    /**
     * 新增客户优惠等级设置
     */
    @Log(title = "客户优惠等级设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerGradeSetting customerGradeSetting)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerGradeSetting.setStationId(currentUser.getUser().getDeptId());
        customerGradeSetting.setCreateBy(currentUser.getUser().getUserId().toString());
        customerGradeSetting.setCreateTime(DateUtils.getNowDate());
        customerGradeSetting.setDelFlag("0");
        return toAjax(customerGradeSettingService.insertCustomerGradeSetting(customerGradeSetting));
    }

    /**
     * 修改客户优惠等级设置
     */
    @Log(title = "客户优惠等级设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerGradeSetting customerGradeSetting)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerGradeSetting.setStationId(currentUser.getUser().getDeptId());
        return toAjax(customerGradeSettingService.updateCustomerGradeSetting(customerGradeSetting));
    }

    /**
     * 删除客户优惠等级设置
     */
    @Log(title = "客户优惠等级设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids)
    {
        int i=0;
        if(ids!=null){
            LoginUser currentUser = SecurityUtils.getLoginUser();
            CustomerGradeSetting customerGradeSetting =new CustomerGradeSetting();
            customerGradeSetting.setUpdateBy(currentUser.getUser().getUserId()+"");
            customerGradeSetting.setUpdateTime(DateUtils.getNowDate());
            customerGradeSetting.setDelFlag("1");
            customerGradeSetting.setId(ids);
            i=customerGradeSettingService.updateCustomerGradeSetting(customerGradeSetting);
        }
        return toAjax(i);
    }

    /**
     * 查询等级名称
     */
    @GetMapping("/gradeList")
    public TableDataInfo gradeList(CustomerGradeSetting customerGradeSetting)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = deptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            customerGradeSetting.setStationIdList(list);
            customerGradeSetting.setStationId(null);
        }
        customerGradeSetting.setDelFlag("0");
        List<CustomerGradeSetting> gradeList = customerGradeSettingService.selectCustomerGradeSettingList(customerGradeSetting);
        return getTable(gradeList);
    }
}
